#include "observe.h"
#include "observe_subject.h"
#include "test.h"

class ObserveM_A : public ObserveM
{
public:
    ObserveM_A(ObserveSubject *sub);
    ~ObserveM_A();

    virtual void update(ObserveSubject *sub);
    virtual void printinfo();

private:
    ObserveSubject *_sub;
};

ObserveM_A::ObserveM_A(ObserveSubject *sub)
{
    _sub = sub;

    if(sub != NULL)
    {
        _sub->attatch(this);
    }
}
ObserveM_A::~ObserveM_A()
{
    if(_sub)
    {
        _sub->detatch(this);
        delete _sub;
    }
}
void ObserveM_A::update(ObserveSubject *sub)
{
    _st = sub->GetState();
    printinfo();
}
void ObserveM_A::printinfo()
{
    cout << "ObserveM_A print" << _st << endl;
}

class ObserveM_B : public ObserveM
{
public:
    ObserveM_B(ObserveSubject *sub);
    ~ObserveM_B();

    virtual void update(ObserveSubject *sub);
    virtual void printinfo();

private:
    ObserveSubject *_sub;
};

ObserveM_B::ObserveM_B(ObserveSubject *sub)
{
    _sub = sub;

    if(sub != NULL)
    {
        _sub->attatch(this);
    }
}
ObserveM_B::~ObserveM_B()
{
    if(_sub)
    {
        _sub->detatch(this);
        delete _sub;
    }
}
void ObserveM_B::update(ObserveSubject *sub)
{
    _st = sub->GetState();
    printinfo();
}
void ObserveM_B::printinfo()
{
    cout << "ObserveM_B print" << _st << endl;
}

void observe_test(void)
{
    ConcreteObvSubject *sub = new ConcreteObvSubject();
    ObserveM *obv_a = new ObserveM_A(sub);
    ObserveM *obv_b = new ObserveM_B(sub);

    sub->SetState("old");
    sub->notify();
    sub->SetState("new");
    sub->notify();
}
